package net.sunofbeach.blog.controller.admin;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sunofbeach.blog.pojo.User;
import net.sunofbeach.blog.response.Result;
import net.sunofbeach.blog.response.ResultEnum;
import net.sunofbeach.blog.service.SettingService;
import net.sunofbeach.blog.service.UserService;
import net.sunofbeach.blog.utils.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Bing
 * @since 2021-11-24
 */
@Api(tags = "网站设置")
@RestController
@RequestMapping("/admin/web_size_info")
public class WebSizeInfoAdminApi {

    @Resource
    private SettingService settingService;

    @Resource
    private UserService userService;


    @ApiOperation("获取网站头部信息")
    @PreAuthorize("@permission.admin()")
    @GetMapping("/title")
    public Result getWebSizeTitle() {
        return settingService.getWebSizeTitle();
    }

    @ApiOperation("更新网站title")
    @PutMapping("/title")
    public Result upWebSizeTitle(@RequestParam("title") String title) {
        // title 存入 set_value 字段
        if (StringUtils.isEmpty(title)) {
            return Result.fail().message("网站标题不能为空");
        }
        // 检验当前操作用户的身份
        User currentUser = userService.checkSobUser();
        if (currentUser == null) {
            // 账号未登录
            return Result.build(null, ResultEnum.ACCOUNT_NOT_LOGIN);
        }
        // 判断角色，拥有 role_admin 角色可以操作
        if (!Constants.User.ROLE_ADMIN.equals(currentUser.getRole())) {
            // 权限不足
            return Result.build(null, ResultEnum.PERMISSION_DENIED);
        }
        return settingService.upWebSizeTitle(title);
    }


    @ApiOperation("获取网站seo信息")
    @PreAuthorize("@permission.admin()")
    @GetMapping("/seo")
    public Result getSeoInfo() {
        return settingService.getSeoInfo();
    }


    @ApiOperation("更新网站seo信息")
    @PutMapping("/seo")
    public Result putSeoInfo(String keyWords, String description) {
        // 检验当前操作用户的身份
        User currentUser = userService.checkSobUser();
        if (currentUser == null) {
            // 账号未登录
            return Result.build(null, ResultEnum.ACCOUNT_NOT_LOGIN);
        }
        // 判断角色，拥有 role_admin 角色可以操作
        if (!Constants.User.ROLE_ADMIN.equals(currentUser.getRole())) {
            // 权限不足
            return Result.build(null, ResultEnum.PERMISSION_DENIED);
        }
        if (StringUtils.isEmpty(keyWords)) {
            return Result.fail().message("关键字不能为空");
        }
        if (StringUtils.isEmpty(description)) {
            return Result.fail().message("网站描述不能为空");
        }
        return settingService.putSeoInfo(keyWords, description);
    }


    @ApiOperation("获取网站浏览量")
    @PreAuthorize("@permission.admin()")
    @GetMapping("/viewCount")
    public Result getSizeViewCount() {
        return settingService.getSizeViewCount();
    }
}

